package com.zlq.cn.bootstrap;

import cn.hutool.core.collection.CollectionUtil;
import com.zlq.cn.api.ApiCodeEnum;
import com.zlq.cn.api.ResponseException;
import com.zlq.cn.dto.vo.SignCommonParam;
import com.zlq.cn.entity.FanyiUser;
import com.zlq.cn.entity.User;
import com.zlq.cn.service.un.FanyiUserService;
import com.zlq.cn.service.un.UserService;
import com.zlq.cn.util.ImeiUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Slf4j
@Component
public class SignApiBootstrap {

    private static UserService userService;

    private static FanyiUserService fanyiUserService;

    public SignApiBootstrap(UserService userService, FanyiUserService fanyiUserService) {
        SignApiBootstrap.userService = userService;
        SignApiBootstrap.fanyiUserService = fanyiUserService;
    }

    private static final ThreadLocal<SignCommonParam> fanYiApiCommonParamLocal = new ThreadLocal<>();

    public static void set(SignCommonParam fanYiCommonParam) {
        fanYiApiCommonParamLocal.set(fanYiCommonParam);
    }

    public static SignCommonParam get() {
        return fanYiApiCommonParamLocal.get();
    }

    public static void remove() {
        fanYiApiCommonParamLocal.remove();
    }

    public static User loginUser() {
        SignCommonParam signCommonParam = get();
        Assert.notNull(signCommonParam, "请求参数有误");
        String token = signCommonParam.getToken();
        if (token != null) {
            return userService.findUserByToken(token);
        }
        return null;
    }

    public static User requestUser() {
        User user = loginUser();
        if (user == null) {
            ResponseException.resultError(ApiCodeEnum.TOKEN_INVALID);
        }
        return user;
    }

    public static FanyiUser requestFanYiUser() {
        return requestFanYiUser(Collections.emptyList());
    }

    public static FanyiUser requestFanYiUser(String project) {
        return requestFanYiUser(Collections.singletonList(project));
    }

    public static FanyiUser requestFanYiUser(List<String> projects) {
        SignCommonParam signCommonParam = get();
        log.info("用户信息：" + signCommonParam.getImei());
        if (!ImeiUtil.checkImei(signCommonParam.getImei())) {
            ResponseException.resultError(ApiCodeEnum.TOKEN_INVALID);
        }
        FanyiUser fanyiUser = fanyiUserService.lambdaQuery()
                .eq(FanyiUser::getFanyiUserImei, signCommonParam.getImei())
                .in(CollectionUtil.isNotEmpty(projects), FanyiUser::getProject, projects)
                .one();
        if (fanyiUser == null || (Objects.nonNull(fanyiUser.getIsDisable()) && 1 == fanyiUser.getIsDisable())) {
            ResponseException.resultError(ApiCodeEnum.TOKEN_INVALID);
        }
        fanyiUserService.updateSurplusDay(fanyiUser);
        return fanyiUser;
    }
}
